package com.backend.service.Impl;

import com.backend.common.AddUser;
import com.backend.common.StudentForm;
import com.backend.common.UserPassword;
import com.backend.config.Result;
import com.backend.entity.AssInformation;
import com.backend.mapper.UserMapper;
import com.backend.entity.Student;
import com.backend.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public Result<Student> userLogin(StudentForm student) {

        Student student1=userMapper.userLogin(student.getStuId());
        if(student1!=null){
            if(student1.getStuPassword().equals(student.getStuPassword())) {
                return Result.success("登录成功", student1);
            }else {
                return Result.success("密码错误",0);
            }
        }
        return Result.fail("用户不存在",-1);
    }

    @Override
    public Result registUser(AddUser addUser) {
        if(!addUser.getPassword().equals(addUser.getPassword1())){
            return Result.fail("两次输入的密码不一致",0);
        }
        Integer i=userMapper.insertUser(addUser);
        return Result.success("注册成功",i);
    }

    @Override
    public Result addNewAction(String actionId, String stuId) {
        Integer i=userMapper.addNewAction(actionId,stuId);
        return Result.success("待管理员审核",i);
    }

    @Override
    public Result quitClub(String assId, String stuId) {
        Integer i=userMapper.quitClub(assId,stuId);
        return Result.success("成功退出",i);
    }

    @Override
    public Result AddAssAction(String userId, String actionId) {
        return Result.success("加入成功",userMapper.AddAssAction(userId,actionId));
    }

    @Override
    public Result getAllAssAction(String userId) {

        List<Map<String,String>> mapList=userMapper.getAllAssAction();
        for (Map<String,String> map:mapList) {
            Map<String,String> map1=userMapper.getAssWheterByUserId(userId,map.get("ass_id"));
            if(map1!=null){
                map.put("whether",map1.get("def"));
            }else {
                map.put("whether","-1");
            }

        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result getNewAction(String userId) {
        List<Map<String,String>> mapList=userMapper.getNewAction();
        for (Map<String,String> map:mapList) {
            Map<String,String> map1=userMapper.getNewActionUser(userId,map.get("new_action_id"));
            if(map1!=null){
                map.put("whether",map1.get("def"));
            }else {
                map.put("whether","-1");
            }
        }
        return Result.success("查找成功",mapList);
    }

    @Override
    public Result deleteImageByUserId(String userId) {

        return Result.success("删除成功",userMapper.deleteImageByUserId(userId));
    }

    @Override
    public Result getUser(String userId) {
        return Result.success("查找成功",userMapper.getUserById(userId));
    }

    @Override
    public Result updateStudentPassword(UserPassword userPassword) {
//    先判断设置的新密码两次输入一致不一致;
        if(!userPassword.getNewPassword().equals(userPassword.getNewPassword1())){
            return Result.fail("两次输入的新密码不一致，请重新输入",-1);
        }
        Map<String,String> user=userMapper.getUserById(userPassword.getUsername());
        if(!user.get("stu_password").equals(userPassword.getOldPassword())){
            return Result.fail("旧密码输入错误，请重新输入",-1);
        }

        Integer i=userMapper.updateUSerPassword(userPassword);
        return Result.success("修改成功",0);
    }

    @Override
    public Result lookUpUserByAss(String stuId) {

        List<AssInformation> assInformationList=userMapper.queryUserCommunity(stuId);

        return Result.success("查询成功",assInformationList);
    }

    @Override
    public Integer uploadUserImages(String imageName, String userId) {
        return userMapper.uploadUserImages(imageName,userId);
    }

    @Override
    public Result lookUpActionUser(String actionId) {
        List<Map<String, String>> mapList= userMapper.lookUpActionUser(actionId);
        int i=1;
        for (Map<String,String> map:mapList) {
            map.put("id",i+"");
            i++;
        }
        return Result.success("查询成功",mapList);
    }

}
